// PartSortPropertyManager.cpp: implementation of the PartSortPropertyManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PartSortPropertyManager.h"
#include "ProductCatalog.h"
#include "Standard.h"
#include "ProductCatalogManager.h"
#include "PersistentFacade.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PartSortPropertyManager::PartSortPropertyManager()
{

}

PartSortPropertyManager::~PartSortPropertyManager()
{
	m_theQueryObjects.RemoveAll();
}

void PartSortPropertyManager::RegisterQueryObject(PSPQuery *pQuery)
{
	ASSERT(pQuery);
	
	m_theQueryObjects.AddHead(pQuery);
}

void PartSortPropertyManager::RemoveQueryObject(PSPQuery *pQuery)
{
	ASSERT(pQuery);
	PSPQuery *pTmpQuery = NULL;
	POSITION pos;

	pos = m_theQueryObjects.GetHeadPosition();
	while(pos)
	{
		pTmpQuery = m_theQueryObjects.GetNext(pos);
		if (pTmpQuery == pQuery) {
			m_theQueryObjects.RemoveAt(pos);
			return;
		}
	}
}

void PartSortPropertyManager::ReadAllProperty()
{
	ProductCatalog * pCataLog = NULL;
	Standard * pStandard = NULL;

	ProductCatalogManager::Iterator *pIter	= ProductCatalogManager::Instance()->NewIterator();	

	for( pIter->Begin(); !pIter->End(); pIter->Next())
	{
		pCataLog = pIter->Get();
		ASSERT(pCataLog);

		pStandard = ::GetPersistentFacade().GetStandardGenerator()->Generate(pCataLog);
		ASSERT(pStandard);

		PSPSortByStandard * pSortStandard = new PSPSortByStandard;
		ASSERT(pSortStandard);
		
		pSortStandard->SetStandard(pStandard->GetStandard());

		RegisterToQuery(pSortStandard);

		this->Add(pStandard->GetStandard(),pSortStandard);

	}
	
	delete pIter;
}

void PartSortPropertyManager::RegisterToQuery(PSPSortByStandard * pStandard)
{
	ASSERT(pStandard);

	POSITION pos = NULL;
	PSPQuery * pQuery = NULL;

	PSPSortByStandard::Iterator * pIter = pStandard->NewIterator();
	
	for(pIter->Begin(); !pIter->End(); pIter->Next())
	{
		PartSortProperty * pProperty = pIter->Get();
		ASSERT(pProperty);

		pos = m_theQueryObjects.GetHeadPosition();
		while(pos)
		{
			pQuery = m_theQueryObjects.GetNext(pos);
			ASSERT(pQuery);

			pQuery->AddSortProperty(pProperty);
		}
	}

	delete pIter;
}

void PartSortPropertyManager::UnRegisterToQuery(PSPSortByStandard * pStandard)
{
	ASSERT(pStandard);

	POSITION pos = NULL;
	PSPQuery * pQuery = NULL;

	PSPSortByStandard::Iterator * pIter = pStandard->NewIterator();
	
	for(pIter->Begin(); !pIter->End(); pIter->Next())
	{
		PartSortProperty * pProperty = pIter->Get();
		ASSERT(pProperty);

		pos = m_theQueryObjects.GetHeadPosition();
		while(pos)
		{
			pQuery = m_theQueryObjects.GetNext(pos);
			ASSERT(pQuery);

			pQuery->RemoveSortProperty(pProperty);
		}
	}

	delete pIter;
}

void PartSortPropertyManager::RemovePropertyByStandard(const CString& strStandard)
{
	ASSERT(!strStandard.IsEmpty());
	
	UnRegisterToQuery(this->GetManaged(strStandard));
	
	this->DeleteItem(strStandard);

}

void PartSortPropertyManager::ReadPropertyByStandard(const CString& strStandard)
{
	ASSERT(!strStandard.IsEmpty());

	PSPSortByStandard* pStandard = NULL;

	pStandard = this->GetManaged(strStandard);
	if(pStandard != NULL)
		return;

	pStandard = new PSPSortByStandard;
	ASSERT(pStandard);
	BOOL bResult = pStandard->SetStandard(strStandard);
	if(!bResult)
	{
		delete pStandard;
		return;
	}
	RegisterToQuery(pStandard);
	this->Add(strStandard, pStandard);
}
